package com.itheima.sercice.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.sercice.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增体检套餐
     * @param setmeal
     * @return
     */
    @Override
    @Transactional
    public void add(Setmeal setmeal, List<Integer> checkgroupIds) {
        setmealDao.add(setmeal);

        //加入数据库之后添加到redis中
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

        Integer setmealId = setmeal.getId();
        //添加套餐和检查组的多对多关系
        if(checkgroupIds!=null&&checkgroupIds.size()>0){
            for (Integer checkgroupId : checkgroupIds) {
                Map map= new HashMap<>();
                map.put("setmeal_id",setmealId);
                map.put("checkgroup_id",checkgroupId);
                setmealDao.setSetmealCheckGroup(map);
            }
        }


    }

    /**
     * 根据套餐id删除套餐
     * @param id
     * @return
     */
    @Override
    public void delete(Integer id) {
        setmealDao.deleteAssocication(id);
        setmealDao.delete(id);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    /**
     * 修改检查套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @Override
    public void edit(Setmeal setmeal, List<Integer> checkgroupIds) {
        //删除原有检查组和套餐关联关系
        setmealDao.deleteAssocication(setmeal.getId());
        //重新设置检查组和检查项的关联关系
        if(checkgroupIds!=null&&checkgroupIds.size()>0){
            for (Integer checkgroupId : checkgroupIds) {
                Map map = new HashMap<>();
                map.put("setmeal_id",setmeal.getId());
                map.put("checkgroup_id",checkgroupId);
                setmealDao.setSetmealCheckGroup(map);
            }
        }
        //更新检查组基本信息
        setmealDao.update(setmeal);
    }

    /**
     * 根据项目编码/名称/助记码分页查询检查套餐
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult PageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<CheckGroup> page=setmealDao.PageQuery(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }
}
